package com.zhejiangzhengyuan.municipal_common.controller;

import com.alibaba.excel.EasyExcel;
import com.zhejiangzhengyuan.municipal_common.config.MessageServer;
import com.zhejiangzhengyuan.municipal_common.entity.Device;
import com.zhejiangzhengyuan.municipal_common.entity.DevicePo;
import com.zhejiangzhengyuan.municipal_common.entity.WarnMessage;
import com.zhejiangzhengyuan.municipal_common.service.DeviceService;
import com.zhejiangzhengyuan.municipal_common.service.InspectionTasksService;
import com.zhejiangzhengyuan.municipal_common.service.WarnMessageService;
import com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.DeviceResolveListener;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.ExcelResolveListener;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author lin_ju
 * @since on 2019/11/29
 */
@RestController
@Slf4j
@Validated
public class TestController {
  @Autowired private WarnMessageService warnMessageService;
  @Autowired private DeviceService deviceService;
  @Autowired private MessageServer messageServer;
  @Autowired private InspectionTasksService inspectionTasksService;
  //  @Autowired private UserService userService;

  //  @ApiOperation("生成文件并下载示例")
  //  @RequestMapping(value = "/testUser", method = RequestMethod.GET)
  //  public void testUser(HttpServletResponse response) {
  //    List<User> users = userService.list();
  //    ExcelUtil.objectToExcelAndDownload(response, "userTest", User.class, users);
  //  }
  //
  //  @ApiOperation("导入文件")
  //  @RequestMapping(value = "/testUser", method = RequestMethod.POST)
  //  pgiublic ResponseVo testUser(@RequestParam("file") MultipartFile file) {
  //    //lister 需在controller或在service定义并确定范型
  //     return ExcelUtil.readExcel(file,User.class,new ExcelResolveListener<User>(userService));
  //  }
  //  @Autowired private WarnEventService warnEventService;
  //
  //  @ApiOperation("生成告警事件图表")
  //  @RequestMapping(value = "/warnEventChart", method = RequestMethod.GET)
  //  public ResponseVo warnEventChart(
  //      @ApiParam(value = "开始时间 格式为 年-月-日", required = true) @NotBlank( message = "startTime
  // 不能为null,格式为 年-月-日") String startTime,
  //      @ApiParam(value = "结束时间 格式为 年-月-日", required = true) @NotBlank( message = "endTime
  // 不能为null,格式为 年-月-日") String endTime) {
  //    return warnEventService.warnEventChart(startTime, endTime);
  //  }
  // 测试长链接  消息推送入口
  //  @RequestMapping(value = "/test")
  //  public void testMessage() {
  //    WarnMessage warnMessage = new WarnMessage();
  //    warnMessage.setReceiveUserId(19L);
  //    warnMessage.setWarnEventId(8L);
  //    warnMessage.setCreateTime(DateFormatUtil.timeFormat(System.currentTimeMillis()));
  //    warnMessageService.save(warnMessage);
  //  }
  @ApiOperation("根据excel修改井盖设备信息")
  @PostMapping(value = "/updateManholeDevice")
  public void updateManholeDevice(MultipartFile file) {
    try {
      EasyExcel.read(
              file.getInputStream(), DevicePo.class, new DeviceResolveListener(deviceService))
          .sheet()
          .doRead();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @ApiOperation("导入井盖设备信息")
  @PostMapping(value = "/importManholeDevice")
  public void importDevice(MultipartFile file) {
    try {
      EasyExcel.read(file.getInputStream(), Device.class, new ExcelResolveListener<>(deviceService))
          .sheet()
          .doRead();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @RequestMapping("/testMessage")
  public void testMessage() {
    WarnMessage warnMessage =
        new WarnMessage()
            .setReceiveUserId(1L)
            .setModuleName("municipal_manhole_cover")
            .setWarnEventId(159L)
            .setCreateTime(DateFormatUtil.timeFormat(System.currentTimeMillis()));
    warnMessageService.save(warnMessage);
    messageServer.refreshSocketMessage("municipal_manhole_cover", 1L);
  }
}
